package LeetCode刷题;

import java.util.Arrays;
import java.util.Collections;

/**
 * @program: Java_Study
 * @author: Xiaofan
 * @createTime: 2021-11-15 14:39
 * @description: Functions of this class is
 * class Solution {
 *     public String reverseWords(String s) {
 *         s=new StringBuilder(s).reverse().toString();
 *         String strs[]=s.split(" ");
 *         int len=strs.length;
 *         String ans=strs[len-1];
 *         for(int i=len-2;i>=0;i--){
 *             ans+=" "+strs[i];
 *         }
 *         return ans;
 *     }
 * }
 **/
public class 反转字符串中的单词III {
    public static void main(String args[]){
        String s="Let's take LeetCode contest";
        System.out.println(reverseWords(s));
    }
    public static String reverseWords(String s) {
        char c[]=s.toCharArray();
        int len=c.length;
        int left=0;
        for(int i=0;i<len;i++){
            if(c[i]==' '||i==len-1){
               int right=i-1;
               while(left<right){
//                   System.out.println(c[left]+"-"+c[right]);
                   char temp=c[left];
                   c[left]=c[right];
                   c[right]=temp;
                   ++left;
                   --right;
               }
               left=i+1;
            }
        }
        s="";
        for(char cc:c){
            s+=cc;
        }
        return s;
    }
}